package com.zlk.algorithm.huawei.leetcode.list;

import com.zlk.algorithm.util.ListNode;
import org.junit.Test;

/**
 * @program: algorithm
 * @ClassName Code20_mergeTwoLists
 * @description:
 * @author: slfang
 * @create: 2025-01-07 16:46
 * @Version 1.0
 **/
public class Code20_mergeTwoLists {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }

        ListNode cur1 =list1;
        ListNode cur2 =list2;
        //ListNode ans = cur1.val>=cur2.val?cur1:cur2;
        ListNode ans ;
        if(cur1.val<=cur2.val){
            ans = cur1;
            cur1 = cur1.next;
        }else{
            ans = cur2;
            cur2 = cur2.next;
        }
        ListNode cur = ans;
        while (cur1!=null&&cur2!=null){
            if(cur1.val<=cur2.val){
                cur.next = cur1;
                cur1 = cur1.next;
            }else{
                cur.next = cur2;
                cur2 = cur2.next;
            }
            cur = cur.next;
        }
        while (cur1!=null){
            cur.next = cur1;
            cur1 = cur1.next;
            cur = cur.next;
        }
        while (cur2!=null){
            cur.next = cur2;
            cur2 = cur2.next;
            cur = cur.next;
        }
        return ans;
    }

    @Test
    public void test(){
        ListNode headA = new ListNode(-9);
        headA.next = new ListNode(3);
        //headA.next.next = new ListNode(4);
        ListNode headB = new ListNode(5);
        headB.next = new ListNode(7);
        //headB.next.next = new ListNode(4);
        mergeTwoLists(headA,headB);
    }
}
